<?php
/**
 * @title 529. 扫雷游戏 - 迭代
 * @author start2004
 */

// ini_set("memory_limit", "128M");


class Solution {

    /**
     * @param String[][] $board
     * @param Integer[] $click
     * @return String[][]
     */
    function updateBoard($board, $click) {
        /**
         * @since 2020-08-20 宽, 高
         */
        $width = count($board[0]);
        $height = count($board);

        /**
         * @since 2020-08-20 第一次click扫到雷
         */
        list($h, $w) = $click;
        if($board[$h][$w] == "M"){
            $board[$h][$w] = "X";
        } else {
            /**
             * @since 2020-08-20 迭代
             */
            $nexts = [$click];
            while($nexts){
                $nextsTmp = [];

                /**
                 * @since 2020-08-20 遍历当前点
                 */
                foreach ($nexts as list($h, $w)){
                    /**
                     * @since 2020-08-20 只有第1次click会挖出地雷
                     * @since 2020-08-20 防止重复处理
                     */
                    if($board[$h][$w] == "E"){
                        /**
                         * @since 2020-08-20 返回周围地雷总数量
                         */
                        $mineCount = 0;

                        /**
                         * @since 2020-08-20 未知领域坐标
                         */
                        $unknowAreaArray = [];

                        /**
                         * @since 2020-08-20 周围8个点
                         */
                        $nears = [
                            [$h-1, $w-1],
                            [$h-1, $w],
                            [$h-1, $w+1],

                            [$h, $w-1],
                            [$h, $w+1],

                            [$h+1, $w-1],
                            [$h+1, $w],
                            [$h+1, $w+1],
                        ];

                        /**
                         * @since 2020-08-20 遍历8个点
                         */
                        foreach ($nears as list($nearHigh, $nearWidth)){
                            if(isset($board[$nearHigh][$nearWidth])){
                                switch ($board[$nearHigh][$nearWidth]){
                                    case "X":
                                        $mineCount++;
                                        break;
                                    case "M":
                                        $mineCount++;
                                        $unknowAreaArray[] = [$nearHigh, $nearWidth];
                                        break;
                                    case "E":
                                        $unknowAreaArray[] = [$nearHigh, $nearWidth];
                                        break;
                                    default:
                                }
                            } else {}
                        }

                        /**
                         * @since 2020-08-20 附近总雷数是0, 附近未知点进入一下轮
                         */
                        if($mineCount == 0){
                            /**
                             * @since 2020-08-20 未知区域进入下一轮循环
                             */
                            $nextsTmp = array_merge($nextsTmp, $unknowAreaArray);

                            /**
                             * @since 2020-08-20 改变点值
                             */
                            $board[$h][$w] = "B";
                        } else {
                            $board[$h][$w] = strval($mineCount);
                        }
                    } else {}
                }

                /**
                 * @since 2020-08-20 下次循环点
                 */
                $nexts = $nextsTmp;
            }
        }

        /**
         * @return
         */
        return $board;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202008/2020.08.20.php
 */
$datas = [
    [
        [
            ['E', 'E', 'E', 'E', 'E'],
            ['E', 'E', 'M', 'E', 'E'],
            ['E', 'E', 'E', 'E', 'E'],
            ['E', 'E', 'E', 'E', 'E'],
        ],
        [3,0],
    ],

    [[["E","E","E","E","E","E","E","E"],["E","E","E","E","E","E","E","M"],["E","E","M","E","E","E","E","E"],["M","E","E","E","E","E","E","E"],["E","E","E","E","E","E","E","E"],["E","E","E","E","E","E","E","E"],["E","E","E","E","E","E","E","E"],["E","E","M","M","E","E","E","E"]], [0,0],],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof(2);
foreach ($datas as $data){
    // var_dump($data);

    $obj = new Solution();
    $result = $obj->updateBoard($data[0], $data[1]);
    var_dump($result);

    // echo "[";
    // foreach ($result as $v){
    //     echo "[", implode(", ", $v) ,"], ", PHP_EOL;
    // }
    // echo "]";
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat("<br>", 3);
}
$xhprof->end();
